# -*- coding: utf-8 -*-
"""
@time：  2021/06/19  15:28
@Auto：Mr. xingye  993843278
@Company 星野训练营 99384378@qq.com
@Function:logging日志打印模块化封装

"""
import logging
import yaml
import os


#获取项目的根目录路径
path = os.path.dirname(os.path.dirname(__file__))



with open(file=path +"/conf/conf.yaml",mode='r',encoding='utf8') as file:
    logging_yaml = yaml.load(stream=file, Loader=yaml.FullLoader)['logger']

    handler = [logging.FileHandler(filename=path + logging_yaml['filename'],mode=logging_yaml['filemode'],encoding='utf8')]
    logging_yaml.pop('filename')
    logging_yaml.pop('filemode')
    # 配置logging日志：主要从文件中读取handler的配置、formatter（格式化日志样式）、logger记录器的配置
    logging.basicConfig(**logging_yaml,handlers=handler)

# 获取根记录器：配置信息从yaml文件中获取，只会输出到日志文件
logger = logging.getLogger()
# 创建输出到控制台的输出流
console = logging.StreamHandler()
# 设置日志等级
console.setLevel(logging_yaml['level'])
# 设置日志格式
console.setFormatter(logging.Formatter(logging_yaml['format']))
# 添加到logger输出
logger.addHandler(console)

if __name__ == '__main__':
    # 等级顺序
    logger.debug("DEBUG")
    logger.info("INFO")
    logger.warning('WARNING')
    logger.error('ERROR')
    try:
        int('a')
    except Exception as e:
        logging.exception(e)